package controller

import (
	"admin/model"
	"common/helper"
	"encoding/json"

	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"net/url"
)

type PromoDepositController struct{}

// 充值配置新增
func (that PromoDepositController) ConfigInsert(ctx *fasthttp.RequestCtx) {

	param := model.PromoDepositConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	param.Name, _ = url.PathUnescape(param.Name)

	err = model.PromoDepositConfigInsert(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

// 充值配置列表
func (that PromoDepositController) ConfigList(ctx *fasthttp.RequestCtx) {

	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")
	id := string(ctx.QueryArgs().Peek("id"))
	ty := string(ctx.QueryArgs().Peek("ty"))
	ex := g.Ex{}
	if id != "" {
		ex["id"] = id
	}
	if ty != "" {
		ex["ty"] = ty
	}
	s, err := model.PromoDepositConfigList(uint(page), uint(pageSize), ex)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, s)
}

// 充值配置更新
func (that PromoDepositController) ConfigUpdate(ctx *fasthttp.RequestCtx) {

	param := model.PromoDepositConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.Name, _ = url.PathUnescape(param.Name)

	err = model.PromoDepositConfigUpdate(param.Id, param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

func (that PromoDepositController) ConfigDelete(ctx *fasthttp.RequestCtx) {

	param := model.PromoDepositConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.Name, _ = url.PathUnescape(param.Name)

	err = model.PromoDepositConfigDelete(param.Id, param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}
